package com.tyjt.common.mqtt.config;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class DataHandlerManager {

    private static final Map<String, DataHandler> map = new HashMap<>();
    private static final Map<String, DataHandler> pattern2Handler = new HashMap<>();

    private static final DataHandlerManager INSTANCE = new DataHandlerManager();

    private DataHandlerManager() {
    }

    public static DataHandlerManager getINSTANCE() {
        return INSTANCE;
    }

    public void setHandler(String topic,DataHandler handler) {

        map.put(topic, handler);
        if (topic.startsWith("$share")) {
            topic = topic.substring(topic.indexOf("/", 7) + 1);
        }
        if (topic.startsWith("$queue")) {
            topic = topic.substring(topic.indexOf("/"));
        }
        pattern2Handler.put(topic, handler);

    }

    public DataHandler getHandler(String pattern) {
        return pattern2Handler.get(pattern);
    }

    public Set<String> patternSet() {
        return pattern2Handler.keySet();
    }

    public String[] topics() {
        Set<String> strings = map.keySet();
        return strings.toArray(new String[0]);

    }


}
